<template>
  <div class="movie">
    <div
      class="cont"
      v-for="item in movieList"
      :key="item._id"
      @click="goToDetail(item.movieid)"
    >
      <div class="main">
        <div class="avatar">
          <div class="imgwarp">
            <img :src="item.img | format" alt="" />
          </div>
        </div>

        <div class="avatar-text">
          <div class="column">
            <div class="movie-title">
              <div class="title">{{ item.nm }}</div>
              <img
                src="../../assets/images/2dimax.png"
                alt=""
                v-if="item.version == 'v2d imax'"
              />
              <img
                src="../../assets/images/v3d.png"
                alt=""
                v-else-if="item.version == 'v3d imax'"
                style="width: 17px"
              />
            </div>

            <div class="detail">
              <div class="score" v-if="item.sc">
                <span>观众评：</span>
                <span class="grade">{{ item.sc }}</span>
              </div>
              <div class="score" v-else-if="item.movieid">
                <span class="grade">{{ item.movieid }}</span>
                <span>:人想看</span>
              </div>
              <div class="score" v-else>
                <span>暂无评分</span>
              </div>

              <div class="actor">主演:{{ item.star }}</div>

              <div class="show-info">{{ item.showInfo }}</div>
            </div>
          </div>

          <div class="button-block">
            <div class="btn" v-if="item.showst == 3">
              <div class="fix">购票</div>
            </div>
            <div class="btn" v-else style="background: rgb(60, 159, 230)">
              <div class="fix">预售</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <p v-if="movieList.length >= count && count">我也是有底线的</p>
  </div>
</template>

<script>
import { moviesApi, movieMoreApi } from "../../utils/api";
export default {
  // props: {
  //   num: Number,
  // },
  data() {
    return {
      movieList: [],
      ids: [],
      count: 0,
      // 表示每次请求多少条
      size: 6,
      // 从那一条开始截取
      startIndex: 0,
    };
  },

  // watch: {
  //   num() {
  //     console.log(1);
  //   },
  // },

  mounted() {
    this.getData();
  },

  methods: {
    async getData() {
      const res = await moviesApi();
      this.movieList = res.result;

      // 获得ids总条数
      this.ids = res.ids;
      this.count = res.count;
      this.startIndex = res.result.length;

      await this.$nextTick();

      // 触发父组件事件
      // 请求结束了 告知父组件去添加bs
      this.$emit("addbs");
    },

    async getMore() {
      const ids = this.ids.slice(this.startIndex, this.startIndex + this.size);
      // console.log(ids);
      const str = ids.join(",");

      const res = await movieMoreApi({
        ids: str,
      });
      // console.log(res);
      // 将拿到的数据拼接到原数据上面
      this.movieList = [...this.movieList, ...res.result];
      this.startIndex += this.size;

      await this.$nextTick();

      this.$emit("finish", this.movieList.length >= this.count);

      // 去请求加载更多的数据接口
      // console.log("getMore");
      // fetch("http://www.pudge.wang:3080/api/movies/more", {
      //   method: "POST",
      //   headers: {
      //     "Content-Type": "application/json",
      //   },
      //   body: JSON.stringify({ ids: "247294" }),
      // })
      //   .then((response) => response.json())
      //   .then((res) => {
      //     console.log(res);
      //   });
    },

    goToDetail(id) {
      this.$router.push("/detail/" + id);
      // console.log(id);
    },
  },

  filters: {
    format(val) {
      return val.split("/w.h").join("/64.90");
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../assets/css/var.less";
.movie {
  width: 100%;
  margin-top: 15px;
  background-color: #fff;

  .cont {
    width: 100%;
    height: 114px;
    padding-left: 15px;

    .main {
      position: relative;
      width: 100%;
      overflow: hidden;

      .avatar {
        width: 64px;
        height: 95px;
        margin-top: 12px;
        float: left;

        .imgwarp {
          width: 64px;
          height: 90px;

          img {
            width: 100%;
            height: 100%;
            background-color: #fff;
          }
        }
      }

      .avatar-text {
        width: 100%;
        height: 104px;
        padding: 12px 14px 12px 0;
        margin-left: 74px;

        .column {
          padding-right: 5px;
          margin-right: 48;
          overflow: hidden;

          .movie-title {
            display: flex;
            height: 24px;
            margin-bottom: 7px;

            .title {
              height: 24px;
              padding-right: 5px;
              flex-shrink: 1;
              font-size: 17px;
              font-weight: 700;
              line-height: 24px;
              color: #333;
            }
            img {
              width: 43px;
              height: 14px;
              margin: 5px 3px 0 0;
              flex: 0 0 auto;
            }
          }

          .detail {
            box-sizing: border-box;
            line-height: 1;
            overflow: hidden;

            .score {
              .ell();
              font-size: 13px;
              color: #666;

              .grade {
                font-size: 15px;
                color: #faaf00;
                font-weight: 700;
              }
            }

            .actor {
              font-size: 13px;
              color: #666;
              .ell();
              margin-top: 6px;
              line-height: 15px;
            }
            .show-info {
              font-size: 13px;
              color: #666;
              .ell();
              margin-top: 6px;
              line-height: 15px;
            }
          }
        }

        .button-block {
          position: absolute;
          top: 0;
          bottom: 0;
          right: 14px;
          font-size: 12px;
          height: 27px;
          line-height: 18px;
          margin: auto;

          .btn {
            background-color: #fff;
            width: 54px;
            height: 28px;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: @theme-color;
            border-radius: 13px;
            .fix {
              color: #fff;
              font-size: 13px;
              font-weight: 500;
            }
          }
        }
      }
    }
  }
}
</style>
